package presale_house.handler;

import presale_house.entity.ClientDo;
import presale_house.service.ClientService;
import presale_house.service.impl.ClientServiceImpl;
import presale_house.show.MainView;
import presale_house.show.view.AddClientView;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AddClientViewHandler implements ActionListener {
    private AddClientView addClientView;
    private MainView mainView;
    public AddClientViewHandler(AddClientView addClientView, MainView mainView){
        this.addClientView = addClientView;
        this.mainView=mainView;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton=(JButton) e.getSource();
        String text=jButton.getText();
        if("添加".equals(text)){
            ClientService clientService =new ClientServiceImpl();
            ClientDo clientDo = addClientView.buildClient();
            System.out.println(clientDo);
            boolean addResult= clientService.add(clientDo);
            if(addResult){
                //更新表格数据
                mainView.reloadTable();
                JOptionPane.showMessageDialog(addClientView,"添加成功");
                addClientView.dispose();
            }else{
                JOptionPane.showMessageDialog(addClientView,"添加失败");
            }
        }
    }
}
